import random

import numpy as np
import pandas as pd

# iteration times of simulation
from osc01.static.constants import Constants

SIMULATIONS = 5
# Array to record time when failures occurs,
# the left column is for seed
times_of_failures = np.zeros((20, SIMULATIONS + 1), dtype=int)
# simulate
for seed in range(0, 20):
    random.seed(seed + 1)
    # time of failure
    time_of_failure = 0
    times_of_failures[seed, 0] = seed + 1
    for col in range(SIMULATIONS):
        # create Time Between Failure
        next_time_between_failure = int(random.expovariate(1 / Constants.CRANE_MTTF))
        time_of_failure += next_time_between_failure
        times_of_failures[seed, col + 1] = time_of_failure
# header
df = pd.DataFrame(times_of_failures)
# table title
title_df = pd.DataFrame(['The crane failure times under scenarios with various random seeds'])
header = ['Seed', 'Failure hour 1', 'Failure hour 2', 'Failure hour 3', 'Failure hour 4', 'Failure hour 5']
# column title
column_title_df = pd.DataFrame([header])
# merge three df
final_df = pd.concat([title_df, column_title_df, df], ignore_index=True)

# save DataFrame to CSV file
final_df.to_csv('Sampled_crane_failures.csv', header=False, index=False)

# np.savetxt('Sampled_crane_failures.csv', failures, delimiter=',', header=''.join(header), fmt='%.0f')
# mean = sum(failures[:, 0]) / len(failures)
# std_dev = np.sqrt(sum((x - mean) ** 2 for x in failures) / len(failures))
# print(f"无故障工作时间的平均值为 {mean} 小时，标准差为 {std_dev} 小时")
